package cn.changeforyou.web.cloud.auth.service.impl;

import cn.changeforyou.web.cloud.auth.datastruct.OrgTree;
import cn.changeforyou.web.cloud.auth.entity.SourceOrgControl;
import cn.changeforyou.web.cloud.auth.entity.enums.OrgControlType;
import cn.changeforyou.web.cloud.auth.manager.AuthCacheManager;
import cn.changeforyou.web.cloud.auth.mapper.SourceOrgControlMapper;
import cn.changeforyou.web.cloud.auth.service.SourceOrgControlService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Service
public class SourceOrgControlServiceImpl extends ServiceImpl<SourceOrgControlMapper, SourceOrgControl>
    implements SourceOrgControlService{

    @Resource
    private AuthCacheManager authCacheManager;

    @Override
    public List<SourceOrgControl> getSourceOrgControlList(Integer orgInfoId) {
        List<SourceOrgControl> list = getBaseMapper().listJoinSource();
        List<SourceOrgControl> result = new ArrayList<>();
        if(list.isEmpty()){
            return result;
        }
        OrgTree orgTree = authCacheManager.getOrgTree();
        for (SourceOrgControl sourceOrgControl : list) {
            OrgControlType orgControlType = sourceOrgControl.getOrgControlType();
            Integer orgId = sourceOrgControl.getOrgId();
            switch (orgControlType) {
                case at:
                    if (orgTree.isFirstAtSecond(orgId, orgInfoId)) {
                        result.add(sourceOrgControl);
                        continue;
                    }
                    break;
                case up:
                    if(orgTree.isFirstUpSecond(orgInfoId, orgId)){
                        result.add(sourceOrgControl);
                        continue;
                    }
                    break;
                case down:
                    if(orgTree.isFirstDownSecond(orgInfoId, orgId)){
                        result.add(sourceOrgControl);
                        continue;
                    }
                    break;
                case upAt:
                    if(orgTree.isFirstUpAtSecond(orgInfoId, orgId)){
                        result.add(sourceOrgControl);
                        continue;
                    }
                    break;
                case downAt:
                    if(orgTree.isFirstDownAtSecond(orgInfoId, orgId)){
                        result.add(sourceOrgControl);
                        continue;
                    }
                    break;
                default:
            }
        }
        return result;
    }
}




